#include "Camera.h"
#include <fstream>
using namespace std;

CCamera::CCamera( mString a_sFileName )
{
    m_bEnabled = true;
    mFloat fFovyDeg;
    mFloat fAspect;
    mFloat fNearDis;
    mFloat fFarDis;
    ifstream Input(a_sFileName.c_str());
    Input >> fFovyDeg;
    Input >> fAspect;
    Input >> fNearDis;
    Input >> fFarDis;
    GenViewMtx();
	m_mProj.MakePerspectiveDeg(fFovyDeg, fAspect, fNearDis, fFarDis);
}

CCamera::~CCamera()
{

}

maths::CVector4f& CCamera::Pos( )
{
    return m_mView.Pos();
}

maths::CVector4f& CCamera::Dir( )
{
    return m_mView.Dir();
}

mVoid CCamera::SetEnabled( mBool a_bEnable )
{
    m_bEnabled = a_bEnable;
}

mBool CCamera::Enabled( )
{
    return m_bEnabled;
}

mVoid CCamera::GenViewMtx()
{
	m_mView = m_L2W;
    m_mView.InvertTransform();
}

mVoid CCamera::GenProjMtx(const mFloat a_fFovyDeg, const mFloat a_fAspect, const mFloat a_fNearDis, const mFloat a_fFarDis)
{
	m_mProj.MakePerspectiveDeg(a_fFovyDeg, a_fAspect, a_fNearDis, a_fFarDis);
}

maths::CPointArray *CCamera::GetFrustumPoints( )
{
    return &m_FrustumPoints;
}

mVoid CCamera::SendViewProj( CIShader *a_pIShader )
{
    a_pIShader->SendConstantValue("g_mViewProj", &m_mViewProj, sizeof(m_mViewProj), EveryFrame);
}

mVoid CCamera::SendView( CIShader *a_IShader )
{
    a_IShader->SendConstantValue("g_mView", &m_mView, sizeof(m_mView), EveryFrame);
}

mVoid CCamera::SendProj( CIShader *a_IShader )
{
    a_IShader->SendConstantValue("g_mProj", &m_mProj, sizeof(m_mProj), EveryFrame);
}

mVoid CCamera::Update( )
{
    GenViewMtx();
    m_mViewProj = m_mView * m_mProj;
    static maths::CAABox UnitCube(maths::CVector4f(-1.0f, -1.0f, 0.0f, 1.0f), maths::CVector4f(1.0f, 1.0f, 1.0f, 1.0f));
    UnitCube.GetPoints(m_FrustumPoints);
    m_FrustumPoints.Transform(m_mViewProj);
}